package org.fingersnow.dbdictionary;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.fingersnow.dbdictionary.dto.Column;
import org.fingersnow.dbdictionary.dto.DataBase;
import org.fingersnow.dbdictionary.dto.Table;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;

/**
 * 生成excel
 * @author dingy
 */
public class Template2Excel {

	
	private String outPutFilePathName = "数据库字典.xlsx";
	private DataBase dataBase;
	
	public Template2Excel(String outPutFilePathName,DataBase dataBase ) {
		this.outPutFilePathName = outPutFilePathName;
		this.dataBase = dataBase;
	}
	
	public void templateToCreateExcel() throws FileNotFoundException {
		OutputStream out = new FileOutputStream(this.outPutFilePathName);		
		try {
			ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
			Sheet sheet1 = new Sheet(1, 0, Table.class);
			sheet1.setSheetName("导航");
			writer.write(dataBase.getTables(), sheet1);
			int i = 2;
			for(Table t:this.dataBase.getTables()) {
				Sheet sheet = new Sheet(i++, 0, Column.class);
				sheet.setSheetName(t.getTable_name());
				writer.write(t.getColumns(), sheet);
			}
			writer.finish();
		} catch (Exception e) {
			throw e;
		} finally {
			if(null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}
